package com.lw.vue_shop_server.service.impl;

import com.lw.vue_shop_server.mapper.RoleMapper;
import com.lw.vue_shop_server.po.Role;
import com.lw.vue_shop_server.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    public RoleMapper roleMapper;

    @Override
    public Map getRoleList() {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        List<Role> roles = roleMapper.getRoleList();
        if(roles!=null){
            result.put("data",roles);
            meta.put("msg","获取成功");
            meta.put("status",200);
        }else {
            meta.put("msg","获取失败");
            meta.put("status",501);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map findRoleById(Integer id) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        Role role = roleMapper.findRoleById(id);
        if(role != null){
            result.put("data",role);
            meta.put("msg","获取成功");
            meta.put("status",200);
        }else {
            meta.put("msg","获取失败");
            meta.put("status",501);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map updateRoleById(Role role) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        Integer row = roleMapper.updateRoleById(role);
        if(row>0){
            result.put("data",role);
            meta.put("msg","修改角色信息成功");
            meta.put("status",200);
        }else {
            meta.put("msg","修改角色信息失败");
            meta.put("status",501);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map delRoleById(Integer id) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        Integer row = roleMapper.delRoleById(id);
        if(row>0){
            result.put("data",null);
            meta.put("msg","删除角色成功");
            meta.put("status",200);
        }else {
            meta.put("msg","删除角色失败");
            meta.put("status",501);
        }
        result.put("meta",meta);
        return result;
    }

    @Override
    public Map addRole(Role role) {
        Map<String,Object> result = new HashMap<>();
        Map<String,Object> meta = new HashMap<>();
        Integer row = roleMapper.addRole(role);
        if(row>0){
            result.put("data",null);
            meta.put("msg","添加角色成功");
            meta.put("status",200);
        }else {
            meta.put("msg","添加角色失败");
            meta.put("status",501);
        }
        result.put("meta",meta);
        return result;
    }

}
